﻿using Common.Contract.Authentication.Record;
using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request.CustomerManager;
using Common.Contract.Customer.Response.CustomerManager;
using Common.Contract.Paging;
using Common.Contract.SystemConfig.Record;
using Common.Service.Command;
using Customer.Model.Interface;
using Customer.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace Customer.Core.ServiceCommand.CustomerManager
{
    public class RetrieveCustomerSubSegmentServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveCustomerSubSegmentRequest, RetrieveCustomerSubSegmentResponse>
    {
        #region Properties
        public UserRecord Dao;
        public FilterRecord TextSearch;
        public string Type;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        #endregion
        public RetrieveCustomerSubSegmentServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveCustomerSubSegmentRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.CurrentUser;
            }
            if (request.Filter == null || string.IsNullOrEmpty(request.Filter.SubSegment))
            {
                throw new ArgumentException("Type must be set", "request");
            }
            TextSearch = Request.Filter;
            if (Request.Page != null)
            {
                pageIndex = Request.Page.PageIndex ?? 0;
                pageSize = Request.Page.PageSize ?? 0;
            }
        }

        protected override bool OnExecute(RetrieveCustomerSubSegmentResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            try
            {
                List<CustomerRecord> records = new List<CustomerRecord>();
                List<Customers> listCustomer = new List<Customers>();
                Dictionary<string, BranchRecord> listBranches = Providers.Branchs.RetrieveAllBranches().Select(x => new BranchRecord
                {
                    BranchId = x.BranchId,
                    BranchName = x.BranchNameSme,
                    Region = x.Region
                }).ToList().ToDictionary(x => x.BranchId);
                listCustomer = Providers.Customers.RetrieveCustomerByDao(out totalRecord, Dao, TextSearch, Request.Lang, pageSize, pageIndex);
                if (listCustomer != null)
                {
                    records = listCustomer.Select(x => new CustomerRecord
                    {
                        DaoName = x.DaoName,
                        Dao = x.Dao,
                        Branch = listBranches.ContainsKey(x.BranchId) ? listBranches[x.BranchId] : new BranchRecord(),
                        CIF = x.Cif,
                        Name = x.CusName,
                        VpbIndustry = x.IndustryName,
                        VpbIndustryEn = x.IndustryNameEn,
                        DateOpenCif = x.CusOpenDate.HasValue ? x.CusOpenDate.Value : DateTime.MinValue
                    }).ToList();
                }
                response.Records = records;
                response.Page = new PageRecord
                {
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    TotalRecords = totalRecord
                };
            }
            catch (Exception e)
            {
                isSuccess = false;
                errorMessage = e.Message;
            }

            response.ErrorMessage = errorMessage;
            return isSuccess;
        }
    }
}
